<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="productSn">
        <el-input
          v-model="queryParams.productSn"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市场价" prop="originalPrice">
        <el-input
          v-model="queryParams.originalPrice"
          placeholder="请输入市场价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:productInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:productInfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:productInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:productInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>


      <el-table-column label="商品图片" width="120" align="pic">
        <template slot-scope="scope">
          <img style="height: 80px" :src="scope.row.pic">
        </template>
      </el-table-column>

      <el-table-column label="商品名称" width="150" align="center">
        <template slot-scope="scope">
          <p>{{scope.row.name}}</p>
          <p>商品品牌：{{scope.row.brandName}}</p>
        </template>
      </el-table-column>


      <el-table-column label="价格/货号" width="120" align="center">
        <template slot-scope="scope">
          <p>价格：￥{{ scope.row.originalPrice }}</p>
          <p>货号：{{ scope.row.productSn }}</p>
        </template>
      </el-table-column>



      <el-table-column label="标签" width="120" align="center">
        <template slot-scope="scope">
          上架:
          <el-switch
            v-model="scope.row.publishStatus"
            :active-value="1"
            :inactive-value="0"
            @change="changePublishStatus(scope.row)"
          ></el-switch>
          <br>
          新品:
          <el-switch
            v-model="scope.row.newStatus"
            :active-value="1"
            :inactive-value="0"
            @change="changeNewStatus(scope.row)"
          ></el-switch>
          <br>
          推荐:
          <el-switch
            v-model="scope.row.recommandStatus"
            :active-value="1"
            :inactive-value="0"
            @change="changeRecommandStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="销量" align="center" prop="sale"/>
      <el-table-column label="审核状态" align="center" prop="verifyStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.verifyStatus === 0">未审核</span>
          <span v-if="scope.row.verifyStatus === 1">已审核</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:productInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:productInfo:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-steps :active="active" finish-status="success">
        <el-step title="填写商品信息"></el-step>
        <el-step title="填写商品促销"></el-step>
        <el-step title="填写商品属性"></el-step>
        <el-step title="选择商品关联"></el-step>
      </el-steps>

      <el-form ref="form" :model="form" :rules="rules" v-if="active==0" label-width="80px">
        <el-form-item label="商品分类" prop="productCategoryId">
          <el-input v-model="form.productCategoryId" placeholder="请输入商品分类"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="副标题" prop="subTitle">
          <el-input v-model="form.subTitle" placeholder="请输入副标题"/>
        </el-form-item>
        <el-form-item label="商品品牌" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入商品品牌"/>
        </el-form-item>
        <el-form-item label="商品货号" prop="productSn">
          <el-input v-model="form.productSn" placeholder="请输入商品货号"/>
        </el-form-item>
        <el-form-item label="商品售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品售价"/>
        </el-form-item>
        <el-form-item label="市场价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入市场价"/>
        </el-form-item>
        <el-form-item label="商品库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入商品库存"/>
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位"/>
        </el-form-item>
        <el-form-item label="商品重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入商品重量，默认为克"/>克
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" :rules="rules" v-if="active==1" label-width="80px">
        <el-form-item label="赠送的积分" prop="giftPoint">
          <el-input v-model="form.giftPoint" placeholder="请输入赠送的积分"/>
        </el-form-item>
        <el-form-item label="赠送的成长值" prop="giftGrowth">
          <el-input v-model="form.giftGrowth" placeholder="请输入赠送的成长值"/>
        </el-form-item>
        <el-form-item label="积分购买限制" prop="usePointLimit">
          <el-input v-model="form.usePointLimit" placeholder="积分购买限制"/>
        </el-form-item>

        <el-form-item label="预告商品" prop="previewStatus">
          <el-input v-model="form.previewStatus" placeholder="预告商品"/>
        </el-form-item>
        <el-form-item label="商品上架" prop="publishStatus">
          <el-input v-model="form.publishStatus" placeholder="商品上架"/>
        </el-form-item>
        商品推荐
        <el-form-item label="新品0 1" prop="newStatus">
          <el-input v-model="form.newStatus" placeholder="新品"/>
        </el-form-item>
        <el-form-item label="推荐0 1" prop="newStatus">
          <el-input v-model="form.newStatus" placeholder="推荐"/>
        </el-form-item>

        <el-form-item label="服务保证" prop="serviceIds">
          <el-input v-model="form.serviceIds" placeholder="1-&gt;无忧退货；2-&gt;快速退款；3-&gt;免费包邮"/>
        </el-form-item>
        <el-form-item label="详情标题" prop="detailTitle">
          <el-input v-model="form.detailTitle" placeholder="请输入详情标题"/>
        </el-form-item>
        <el-form-item label="详情页描述" prop="detailDesc">
          <el-input v-model="form.detailDesc" placeholder="详情页描述"/>
        </el-form-item>
        <el-form-item label="商品关键字" prop="keywords">
          <el-input v-model="form.keywords" placeholder="商品关键字"/>
        </el-form-item>
        <el-form-item label="商品备注" prop="note">
          <el-input v-model="form.note" placeholder="商品备注"/>
        </el-form-item>

        <el-form-item label="选择优惠方式:" prop="promotionType">
          <el-tabs v-model="form.promotionType" @tab-click="handleClick">
            <el-tab-pane label="无优惠" name="first">无优惠</el-tab-pane>
            <el-tab-pane label="特惠促销" name="second">
              <el-form-item label="开始时间：" prop="detailHtml">
                <el-date-picker clearable
                                v-model="form.promotionStartTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择促销开始时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="结束时间：" prop="detailHtml">
                <el-date-picker clearable
                                v-model="form.promotionEndTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择促销结束时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="促销价格：" prop="promotionPrice">
                <el-input v-model="form.promotionPrice"/>
              </el-form-item>
            </el-tab-pane>
            <el-tab-pane label="会员价格" name="third">
              <el-form-item label="黄金会员" prop="detailHtml">
                <el-input v-model="form.detailHtml"/>
              </el-form-item>
              <el-form-item label="白金会员" prop="detailHtml">
                <el-input v-model="form.detailHtml"/>
              </el-form-item>
              <el-form-item label="砖石会员" prop="detailHtml">
                <el-input v-model="form.detailHtml"/>
              </el-form-item>

            </el-tab-pane>
            <el-tab-pane label="阶梯价格" name="fourth">阶梯价格</el-tab-pane>
            <el-tab-pane label="满减价格" name="fourth">满减价格</el-tab-pane>
          </el-tabs>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" :rules="rules" v-if="active==2" label-width="80px">
        <el-form-item label="品牌属性分类id" prop="productAttributeCategoryId">
          <el-input v-model="form.productAttributeCategoryId" placeholder="请输入品牌属性分类id"/>
        </el-form-item>


        <el-form-item label="图片" prop="pics">
          <image-upload v-model="form.pics"/>
        </el-form-item>

        <el-form-item label="产品详情网页内容" prop="detailHtml">
          <el-input v-model="form.detailHtml" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="移动端网页详情" prop="detailMobileHtml">
          <el-input v-model="form.detailMobileHtml" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>

      <el-form ref="form" :model="form" :rules="rules" v-if="active==3" label-width="80px">

        <el-form-item label="运费模版id" prop="feightTemplateId">
          <el-input v-model="form.feightTemplateId" placeholder="请输入运费模版id"/>
        </el-form-item>

        <el-form-item label="活动限购数量" prop="promotionPerLimit">
          <el-input v-model="form.promotionPerLimit" placeholder="请输入活动限购数量"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <span v-if="active!=0"><el-button style="margin-top: 12px;" @click="active--">上一步</el-button></span>
        <span v-if="active!=3"><el-button style="margin-top: 12px;" @click="next">下一步</el-button></span>
        <span v-if="active===3"><el-button type="primary" @click="submitForm">完成提交商品</el-button></span>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listProductInfo,
  getProductInfo,
  delProductInfo,
  addProductInfo,
  updateProductInfo
} from "@/api/product/productInfo";

export default {
  name: "ProductInfo",
  data() {
    return {
      active: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      productInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        productSn: null,
        publishStatus: null,
        verifyStatus: null,
        originalPrice: null,
        brandName: null,
        productCategoryName: null
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "商品名称不能为空", trigger: "blur"}
        ],
        productSn: [
          {required: true, message: "货号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    next() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.active++ > 3) this.active = 0;
          console.log(this.active)
          this.choseBox()
        } else {
          return false;
        }
      })
    },
    changeRecommandStatus(params) {
      let data = {
        'id': params.id,
        'recommandStatus': params.recommandStatus
      }
      updateProductInfo(data).then(response => {
        if (response.code === 200) {
          this.$message.success("修改成功")
        }
      })
    },
    changeNewStatus(params) {
      let data = {
        'id': params.id,
        'newStatus': params.newStatus
      }
      updateProductInfo(data).then(response => {
        if (response.code === 200) {
          this.$message.success("修改成功")
        }
      })
    },
    changePublishStatus(params) {
      let data = {
        'id': params.id,
        'publishStatus': params.publishStatus
      }
      updateProductInfo(data).then(response => {
        if (response.code === 200) {
          this.$message.success("修改成功")
        }
      })
    },

    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listProductInfo(this.queryParams).then(response => {
        this.productInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        brandId: null,
        productCategoryId: null,
        feightTemplateId: null,
        productAttributeCategoryId: null,
        name: null,
        pic: null,
        productSn: null,
        deleteStatus: null,
        publishStatus: null,
        newStatus: null,
        recommandStatus: null,
        verifyStatus: null,
        sort: null,
        sale: null,
        price: null,
        promotionPrice: null,
        giftGrowth: null,
        giftPoint: null,
        usePointLimit: null,
        subTitle: null,
        description: null,
        originalPrice: null,
        stock: null,
        lowStock: null,
        unit: null,
        weight: null,
        previewStatus: null,
        serviceIds: null,
        keywords: null,
        note: null,
        albumPics: null,
        detailTitle: null,
        detailDesc: null,
        detailHtml: null,
        detailMobileHtml: null,
        promotionStartTime: null,
        promotionEndTime: null,
        promotionPerLimit: null,
        promotionType: null,
        brandName: null,
        productCategoryName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function () {
        return delProductInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/productInfo/export', {
        ...this.queryParams
      }, `productInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
